package com.tanjun.mallservice.web.service.impl;

import com.mybatisflex.core.query.QueryChain;
import com.mybatisflex.core.update.UpdateChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.tanjun.mallservice.apiservice.enums.plu.PluDescTypeEnum;
import com.tanjun.mallservice.web.orm.entity.MallPluDesc;
import com.tanjun.mallservice.web.orm.mapper.MallPluDescMapper;
import com.tanjun.mallservice.web.service.MallPluDescService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author jjtan
 * @description 针对表【mall_plu_desc(商品说明)】的数据库操作Service实现
 * @createDate 2024-03-22 10:33:44
 */
@Service
public class MallPluDescServiceImpl extends ServiceImpl<MallPluDescMapper, MallPluDesc>
        implements MallPluDescService {

    /**
     * 获取详细说明
     *
     * @param pluId    商品id
     * @param skuId    规格id
     * @param typeEnum 类型
     * @return 详细说明
     */
    @Override
    @Cacheable(value = "getDescInfo", key = "#pluId+':'+#skuId+':'+#typeEnum")
    public String getDescInfo(Long pluId, Long skuId, PluDescTypeEnum typeEnum) {
        MallPluDesc pluDesc = this.getOne(QueryChain.of(MallPluDesc.class)
                .eq(MallPluDesc::getPluId, pluId)
                .eq(MallPluDesc::getDescType, typeEnum)
                .eq(MallPluDesc::getSkuId, skuId,typeEnum == PluDescTypeEnum.SKU)
        );
        if (pluDesc == null) {
            return null;
        }
        return pluDesc.getDescInfo();
    }

    /**
     * 保存详细说明
     *
     * @param pluId    商品id
     * @param skuId    规格id
     * @param typeEnum 类型
     */
    @Override
    @CacheEvict(cacheNames = {"getDescInfo"}, allEntries = true)
    public void saveDescInfo(Long pluId, Long skuId, PluDescTypeEnum typeEnum, String desc) {
        MallPluDesc old = this.getOne(QueryChain.of(MallPluDesc.class)
                .select(MallPluDesc::getPluDescId)
                .eq(MallPluDesc::getPluId, pluId)
                .eq(MallPluDesc::getDescType, typeEnum)
                .eq( MallPluDesc::getSkuId, skuId,typeEnum == PluDescTypeEnum.SKU)
        );
        if (old != null) { //存在旧数据
            if (!StringUtils.hasText(desc)) { //新的为空
                this.removeById(old.getPluDescId());
            } else {
                UpdateChain.of(MallPluDesc.class)
                        .set(MallPluDesc::getDescInfo, desc)
                        .eq(MallPluDesc::getPluDescId, old.getPluDescId())
                        .update();
            }
        } else {
            if (!StringUtils.hasText(desc)) { //新的为空
                return;
            } else {
                MallPluDesc mallPluDesc = new MallPluDesc();
                mallPluDesc.setPluId(pluId);
                mallPluDesc.setSkuId(skuId);
                mallPluDesc.setDescType(typeEnum);
                mallPluDesc.setDescInfo(desc);
                this.save(mallPluDesc);
            }

        }
    }
}




